package brad.zimmerman.dungeon.game;

import java.util.ArrayList;

import brad.zimmerman.dungeon.game.ClassCreateMap.coorObject;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Display;
import android.widget.ImageView;

//Used to combine the gameboard and createmap classes 
//before they are pushed to the dungeon.java class
public class ClassReferenceImage {
	public Integer[] Tiles = {R.drawable.dirt_crypt, R.drawable.wall_crypt};
	int[][] map;
	int[][] playerBoard;
	ClassCreateMap mapObject;
	ClassObjPosition playerStart;
	ClassGameBoard board;
	int tiles;
	public ClassReferenceImage(int tiles, int scale, int width, int height, ClassGameStats playerStats){
		//Log.d("RefImage","Ref started");
		if (playerStats.player_level > 0){
			this.mapObject = new ClassCreateMap(tiles);
		}else{
			this.mapObject = new ClassCreateMap(true);
		}
		this.playerBoard = new int[tiles][tiles];
		this.map = mapObject.returnMap();
		//Log.d("RefImage","Map Created");
		playerStart = new ClassObjPosition(tiles, scale, width, height);
		//Log.d("RefImage","Player Pos Set");
		if (playerStats.player_level > 0){
			board = new ClassGameBoard(map,mapObject.getPlayerX(),mapObject.getPlayerY(), tiles, playerStats);
		}else{
			board = new ClassGameBoard(map,mapObject.getPlayerX(),mapObject.getPlayerY(), true, playerStats);
		}
		this.tiles = tiles;
		//Log.d("RefImage","Ref finished");
	}
	public int[][] returnMap(){
		return this.map;
	}
	public int[][] returnboard(){
		return board.returnMap();
	}
	public int returnPlayerStartX(){
		return playerStart.findXBoard(mapObject.getPlayerX(),mapObject.getPlayerY());
	}
	public int returnPlayerStartY(){
		return playerStart.findYBoard(mapObject.getPlayerX(),mapObject.getPlayerY());
	}
	boolean isWall(int X, int Y){
		if (map[X][Y] == 1){
			return true;
		}
		return false;
	}
	public int shroudNum(int X, int Y){
		return this.board.shroudPos[X][Y];
	}
	public int glyphNum(int X, int Y){
		return this.board.glyphPos[X][Y];
	}
	public void setGlyphNum(int X, int Y, int newVal){
		this.board.glyphPos[X][Y] = newVal;
	}
	public int objNum(int X, int Y){
		return this.board.objPos[X][Y];
	}
	public int shopNum(int X, int Y){
		return this.board.shopPos[X][Y].getItem();
	}
	public ClassShop shopObj(int X, int Y){
		return this.board.shopPos[X][Y];
	}
	public int shopNumTech(int X, int Y){
		return this.board.shopPos[X][Y].getTech();
	}
	public void setShopObj(int X, int Y, int value, int tech){
		this.board.shopPos[X][Y].setItem(value);
	}
	public ClassAltar altarObj(int X, int Y){
		return this.board.altarPos[X][Y];
	}
	public int altarNum(int X, int Y){
		return this.board.altarPos[X][Y].getGod();
	}
	public void setAltarObj(int X, int Y, int value){
		this.board.altarPos[X][Y].setGod(value);
	}
	public void setObjNum(int X, int Y, int newVal){
		this.board.objPos[X][Y] = newVal;
	}
	public int enemyNum(int X, int Y){
		return this.board.enemyPos[X][Y].returnMonster();
	}
	public int[][] returnEnemyMap(){
		int[][] map = new int[tiles][tiles];
		for(int y = 0 ; y < tiles ; y++){
			for(int x = 0 ; x < tiles; x++){
				map[x][y] = this.board.enemyPos[x][y].returnMonster();
			}
		}
		return map;
	}
	public ClassEnemyStats returnEnemyObj(int X, int Y){
		return this.board.enemyPos[X][Y];
	}
	public void setEnemyObj(int X, int Y, ClassEnemyStats value){
		this.board.enemyPos[X][Y] = value;
	}
	public void setShroudObj(int X, int Y, int value){
		this.board.shroudPos[X][Y] = value;
	}
	public int debrisNum(int X, int Y){
		return this.board.debrisPos[X][Y];
	}
	public void setDebrisObj(int X, int Y, int value){
		this.board.debrisPos[X][Y] = value;
	}
	public int countShroudTiles(){
		int count = 0;
		for(int Y = 0; Y<tiles;Y++){
			for(int X = 0; X<tiles;X++){
				if (this.board.shroudPos[X][Y]==2){
					count++;
				}
			}
		}
		return count;
	}
	
	public void upgradeEnemies(int level, int lives){
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(this.board.enemyPos[x][y].identifier !=0){
					if((this.board.enemyPos[x][y].level+level)<=10){
						this.board.enemyPos[x][y].setLevel(this.board.enemyPos[x][y].level+level);
					}else{
						this.board.enemyPos[x][y].setLevel(10);
					}
					this.board.enemyPos[x][y].extraLives+=lives;
				}
			}
		}
	}
	
	public void setLevel(float difficulty){
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(this.board.enemyPos[x][y].identifier !=0){
					this.board.enemyPos[x][y].setDifficulty(difficulty);
				}
			}
		}
	}
	
	public int returnTotems(){
		int count = 0;
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(debrisNum(x, y) ==2){
					count++;
				}
			}
		}

		return count;
	}
	
	public boolean allBossesDead(){
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(this.board.enemyPos[x][y].identifier !=0){
					if((this.board.enemyPos[x][y].level)==10){
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public void regenEnemyHealth(){
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(this.board.enemyPos[x][y].currentHealth < this.board.enemyPos[x][y].health 
						&& this.board.enemyPos[x][y].identifier != 0 && this.board.enemyPos[x][y].ePoisoned == false){
					this.board.enemyPos[x][y].currentHealth += this.board.enemyPos[x][y].level;
					if (this.board.enemyPos[x][y].currentHealth> this.board.enemyPos[x][y].health){
						this.board.enemyPos[x][y].currentHealth= this.board.enemyPos[x][y].health;
					}
				}
			}
		}
	}
	
	public float computeSightBonus(float bonus){
		int count = 0;
		for(int y = 0; y < tiles; y++){
			for(int x = 0; x< tiles; x++){
				if(this.board.enemyPos[x][y].identifier!= 0 && this.board.shroudPos[x][y]==0){
					count++;
				}
			}
		}
		return (count * bonus);
	}
	
	public ClassGameStats updatePlayerSight(ClassGameStats playerStats, int playerX, int playerY){
		for(int y = (playerY - playerStats.returnSight()); y <= (playerY + playerStats.returnSight()); y++ ) {
			for(int x = (playerX - playerStats.returnSight()); x <= (playerX + playerStats.returnSight()); x++ ) {
				if(x>= 0 && x <tiles && y>= 0 && y <tiles){
					if(this.board.shroudPos[x][y] != 0){
						if(playerStats.stepsTaken>0){
							playerStats.shroudRenewal(x, y);
							playerStats.curePlayerAilments();
							//This is the function that regens enemy health
							//It needs to be worked on to be better than O(n^2) time if possible
							regenEnemyHealth();
						}
					}
					this.board.shroudPos[x][y] = 0;
					//playerStats.shroudRenewal();
				}
			}
		}
		return playerStats;
	}
	public boolean exploredAround(int locationX, int locationY){
		ArrayList<Integer> _x = new ArrayList<Integer>();
		ArrayList<Integer> _y = new ArrayList<Integer>();
		
		for(int x = locationX-1;x<=locationX+1;x++){
			if (x>-1 && x<tiles){
				_x.add(x);
			}
		}
		for(int y = locationY-1;y<=locationY+1;y++){
			if (y>-1 && y<tiles){
				_y.add(y);
			}
		}
		
		for(int y = 0; y<_y.size();y++){
			for(int x = 0; x<_x.size();x++){
				if (this.board.shroudPos[_x.get(x)][_y.get(y)] != 0){
					return false;
				}
			}
		}
		return true;
	}
}
